<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NotificationModel
 *
 * @author Admin
 */
class Application_Model_SwitchUserDetailModel extends Zend_Db_Table_Abstract {

    protected $_name = 'switch_user_detail';
    protected $_fields = 'id,switch_id,user_id,month,year,monthlytarget,reached,commission_percent,monthly_max,commission_percent_max';
    public $adapter;

    function init() {
        $this->adapter = $this->getAdapter();
    }

    function save($data) {
        if (isset($data['id'])) {
            return $this->update($data, "id = $data[id]");
        } else {
            return $this->insert($data);
        }
    }

    function getDetail($field, $value) {
        return $this->adapter->fetchRow("select $this->_fields from $this->_name where $field = '$value'");
    }

    function getSwitchHouseTargetStats($switchId) {
        return $this->adapter->fetchRow("select sum(monthlytarget) as total_houses,sum(reached) as total_reached, sum(commission_percent) as commission from $this->_name where switch_id = '$switchId'");
    }

    function getAllUserTargets($request) {
        $select = $this->adapter->select()
                        ->from($this->_name, array('id', 'month', 'year', 'monthlytarget', 'reached', '(monthlytarget - reached) as remaining', 'switch_user_detail.user_id', 'switch_user_detail.switch_id', 'switch_user_detail.commission_percent'
                            , 'switch_user_detail.commission_percent_max', 'switch_user_detail.monthlytarget', 'switch_user_detail.monthly_max'))
                        ->joinInner('switch_mgmt', "switch_user_detail.switch_id = switch_mgmt.id", array('switch_mgmt.name as switchName', 'switch_mgmt.contract_price'))
                        ->WHERE("switch_user_detail.user_id='{$request->getParam('user')}' AND switch_user_detail.switch_id='{$request->getParam('switch')}'");
        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }

    function getAllSwitchUserTargets($request, $userId) {
        $select = $this->adapter->select()
                        ->from($this->_name, array('id', 'month', 'year', 'monthlytarget', 'reached', '(monthlytarget - reached) as remaining', 'switch_user_detail.user_id', 'switch_user_detail.switch_id', 'switch_user_detail.commission_percent'
                            , 'switch_user_detail.commission_percent_max', 'switch_user_detail.monthlytarget', 'switch_user_detail.monthly_max'))
                        ->joinInner('switch_mgmt', "switch_user_detail.switch_id = switch_mgmt.id", array('switch_mgmt.name as switchName', 'switch_mgmt.contract_price'))
                        ->WHERE("switch_user_detail.user_id='{$userId}' AND switch_user_detail.switch_id='{$request->getParam('id')}'");
        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }

}